package com.mzsoft.ai.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;

/**
 * 读取excel模板数据(表字段和excel标题对应字段在配置文件配置)
 * @author libq
 */
public class ReadTemplateData {

    private static Logger logger = LoggerFactory.getLogger(ReadTemplateData.class);

    public static Map<String,Object> readMultiSheetData(String filePath, Map<String, Object> multiSheetFieldMap) {

        //最终结果
        Map<String,Object> result = new HashMap<>();

        //读取单位excel
        try {
            //读取excel文件中多个sheet
            Map<String, Object> excelMultiSheetMap = ExcelUtil.readExcelMultiSheetMap(filePath,1);
            logger.debug("excel数据:"+JSON.toJSONString(excelMultiSheetMap));
            //遍历转换
            if(excelMultiSheetMap != null && !excelMultiSheetMap.isEmpty()){
                for(String sheetNo : excelMultiSheetMap.keySet()){

                    List<Map<String,Object>> list = (List<Map<String, Object>>) excelMultiSheetMap.get(sheetNo);

                    List<Map<String,Object>> sheetList = new ArrayList<>();
                    //遍历转换
                    if(list != null && !list.isEmpty()){
                        for(Map<String, Object> map : list){
                            Map<String,Object> objectMap = new HashMap<>();
                            for(String key : map.keySet()){
                                Map<String,Object> sheetMap = (Map<String, Object>) multiSheetFieldMap.get(sheetNo);

                                Map<String,String> fieldMap = (Map<String, String>) sheetMap.get("column");
                                if(StringUtils.isNotBlank(fieldMap.get(key))){
                                    objectMap.put(fieldMap.get(key),map.get(key));
                                }
                            }
                            sheetList.add(objectMap);
                            result.put(sheetNo,sheetList);
                        }
                    }
                }
            }
            System.out.println("最终结果："+JSON.toJSON(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
